package com.erp.produce.service.impl;

import com.erp.common.util.ServiceUtil;
import com.erp.common.util.StringUtil;
import com.erp.produce.dao.SupplierDao;
import com.erp.produce.entity.Supplier;
import com.erp.produce.service.SupplierService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SupplierServiceImpl implements SupplierService {
    @Autowired
    SupplierDao dao;


    @Override
    public Boolean update(Supplier supplier) {
        Supplier supplier1 = dao.findById(supplier.getId()).get();
        BeanUtils.copyProperties(supplier,supplier1);
        return dao.saveAndFlush(supplier1) != null;
    }

    @Override
    public Boolean add(Supplier supplier) {
        return dao.save(supplier) != null;
    }

    @Override
    public Boolean deleteIds(Long[] ids) {
        return ServiceUtil.deleteIds(ids,dao);
    }

    @Override
    public List<Supplier> listPage(int page, int size) {
        List<Supplier> list = dao.findAll(PageRequest.of(page, size, new Sort(Sort.Direction.ASC, "id"))).getContent();
        return list;
    }

    @Override
    public List<Supplier> listPageLike(int page, int size, String column) {
        return dao.findAllBySupplierNameLike(StringUtil.addLike(column),PageRequest.of(page, size,new Sort(Sort.Direction.ASC,"id")));
    }
}
